package com.fudan.biz.discover.web;

import java.io.IOException;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fudan.es.search.QueryService;
import com.fudan.es.search.SearchResponseEntity;
import com.fudan.framework.entity.ResponseMsg;
import com.fudan.framework.utils.JsonUtils;
import com.fudan.framework.web.controller.PathController;

@Controller
@RequestMapping("/biz/discover")
public class DiscoverController implements PathController{

	@Autowired
	private QueryService queryService;

	@Override
	public String pathPrefix() {
		return "biz/dataview";
	}

	@RequestMapping("/search")
	@ResponseBody
	public ResponseMsg search(HttpServletRequest request) {
		try {
			String query = request.getParameter("query");
			SearchResponseEntity responseEntity = queryService.search(query);
			return ResponseMsg.build().putData(responseEntity.getRecordsAsMap()).put("total", responseEntity.getTotalHit());
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseMsg.build(500,e.getMessage());
		}
	}
}
